﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace TheCup.Controllers
{
    public class PlayerController : Controller
    {
        ApiControllers.PlayerController pc = new ApiControllers.PlayerController();
        ApiControllers.ScoreController sc = new ApiControllers.ScoreController();
        ApiControllers.CourseController cc = new ApiControllers.CourseController();

        public ActionResult Index()
        {
            var players = pc.Get();
            return View(players);
        }

        public ActionResult Details(int id, int? year)
        {
            year = year == null ? DateTime.UtcNow.Year : year;

            Player p = pc.Get(id);
            List<Scorecard> scores = sc.GetScorecardsForPlayer(id, year.GetValueOrDefault()).OrderByDescending(d => d.Round.DateEntry).ToList<Scorecard>();
            ViewBag.scores = scores;
            return View(p);
        }

        [Authorize]
        public ActionResult Edit()
        {
            Player p = pc.GetLoggedInPlayer();
            ViewBag.Sexes = this.GetSexSelectList();
            ViewBag.Courses = cc.Get();
            return View(p);
        }
        
        [HttpPost]
        [Authorize]
        public ActionResult Edit(Player p)
        {
            if (ModelState.IsValid)
            {
                using (var ctx = new TheCupContext())
                {
                    Player pl = ctx.Players.Find(p.Id);
                    
                    // basis info
                    pl.GivenName = p.GivenName;
                    pl.LastName = p.LastName;
                    pl.Sex = p.Sex;
                    pl.Birthdate = p.Birthdate;
                    pl.Address = p.Address;
                    pl.ZipCode = p.ZipCode;
                    pl.MobilePhone = p.MobilePhone;
                    pl.Email = p.Email;
                    pl.MemberNumber = p.MemberNumber;
                    pl.CourseId = p.CourseId;
                    pl.Hcp = p.Hcp;
                    pl.SendEventMails = p.SendEventMails;

                    // udstyr
                    pl.Driver = p.Driver;
                    pl.Irons = p.Irons;
                    pl.Putter = p.Putter;
                    pl.Balls = p.Balls;

                    // ambitioner
                    pl.RoleModel = p.RoleModel;
                    pl.StrongFeatures = p.StrongFeatures;
                    pl.WeakFeatures = p.WeakFeatures;
                    pl.BestRound = p.BestRound;
                    pl.SeasonObjective = p.SeasonObjective;
                    
                    // don't save until we confirm
                    ctx.SaveChanges();
                }
            }
            else
            {
                ViewBag.Courses = cc.Get();
                ModelState.AddModelError("Error", "Du har ingen profil tilknyttet endnu. Er du blevet godkendt af en TheCup Administrator?");
                return View(p);
            }

            return RedirectToAction("Details", new { id = p.Id });
        }

        public IEnumerable<SelectListItem> GetSexSelectList()
        {
            Array values = Enum.GetValues(typeof(Sex));
            List<SelectListItem> items = new List<SelectListItem>(values.Length);
            
            foreach (var i in values)
            {
                items.Add(new SelectListItem
                {
                    Text = Enum.GetName(typeof(Sex), i),
                    Value = i.ToString()
                });
            }

            return items;
        }
    }
}
